﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Project5188.BUS;
using Project5188.BUS.Operations;
using ExportToExcelTools;
using Project5188.Client.ClientCodes;

namespace Project5188.Client.Operations
{
    /// <summary>
    /// Interaction logic for InsuranceInfoPage.xaml
    /// </summary>
    public partial class InsuranceInfoPage
    {
        private CollectionViewSource _resources;
        private readonly long _personnelId;

        public InsuranceInfoPage()
            : base(Extensions.PageNames.InsuranceInfoPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;

            BindData();
        }

        public InsuranceInfoPage(long personnelId)
            : base(Extensions.PageNames.InsuranceInfoPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;

            BindData();

            _personnelId = personnelId;
        }

        private void BindData()
        {
            var parameterValues = new ParameterValues();

            drpAvailabilityWeapon.SelectedValuePath = "ID";
            drpAvailabilityWeapon.IsSynchronizedWithCurrentItem = true;
            drpAvailabilityWeapon.ItemTemplate = Resources["comboTemplate"] as DataTemplate;
            drpAvailabilityWeapon.ItemsSource = parameterValues.GetEntityByParameterId(6);
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            if (((FrameworkElement)(e.OriginalSource)).Name == txtRegistryNo.Name ||
                ((FrameworkElement)(e.OriginalSource)).Name == txtSecurityCredentialsDocumentNo.Name ||
                ((FrameworkElement)(e.OriginalSource)).Name == txtInsuranceInfoPolicyNo.Name)
            {
                e.Handled = !AreAllValidNumericChars(e.Text);
            }
            base.OnPreviewTextInput(e);
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgInsuranceInfos.ItemsSource = _resources.Source as IEnumerable;
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var insert = new InsertWindow();
            insert.Closed += InsertClosed;
            DetailView<insuranceinfo> insuranceInfoDetail = new InsuranceInfos();
            insert.SetEntity(insuranceInfoDetail, Extensions.PageNames.InsuranceInfoPage.ToString(), _personnelId);
            insert.Show();
        }

        public override bool Search()
        {
            if (base.Search())
            {
                var insuranceInfos = new InsuranceInfos();
                _resources = Resources["insuranceInfoViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    insuranceInfos.GetEntityListByParameters(txtRegistryNo.Text, txtNameSurname.Text,
                                                             txtWorkPlaceName.Text,
                                                             txtSecurityCredentialsDocumentNo.Text,
                                                             Convert.ToInt64(drpAvailabilityWeapon.SelectedValue),
                                                             txtInsuranceInfoPolicyNo.Text,
                                                             txtInsuranceInfoCompanyName.Text,
                                                             dtInsuranceInfoStartDate.SelectedDate), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgInsuranceInfos.ItemsSource = _resources.Source as IEnumerable;
            }
            else if (TransactionInfo.TransactionId.HasValue)
            {
                var insuranceInfos = new InsuranceInfos();
                _resources = Resources["insuranceInfoViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    insuranceInfos.GetEntityById(TransactionInfo.TransactionId.Value), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgInsuranceInfos.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var insuranceInfoValue = (insuranceinfo)dgInsuranceInfos.SelectedItem;
            var detail = new DetailWindow();
            DetailView<insuranceinfo> insuranceInfoValueDetail = new InsuranceInfos { Entity = insuranceInfoValue };
            detail.SetEntity(insuranceInfoValueDetail, Extensions.PageNames.InsuranceInfoPage.ToString());
            detail.Show();
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var insuranceInfoValue = (insuranceinfo)dgInsuranceInfos.SelectedItem;
            var update = new UpdateWindow();
            update.Closed += UpdateClosed;
            DetailView<insuranceinfo> insuranceInfoValueDetail = new InsuranceInfos();
            update.SetEntity(insuranceInfoValueDetail, Extensions.PageNames.InsuranceInfoPage.ToString(), insuranceInfoValue.a_ID);
            update.Show();
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            var insuranceInfoValue = (insuranceinfo)dgInsuranceInfos.SelectedItem;
            DetailView<insuranceinfo> insuranceInfoValueDetail = new InsuranceInfos();
            GenerateDeleteDialog(insuranceInfoValueDetail, insuranceInfoValue.a_ID);
        }

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                dgInsuranceInfos.ExportToExcel();
            }
            catch
            {
                MessageBox.Show("Excel'e aktarma sırasında hata oluştu.");
            }
        }
    }
}
